<!--
 * @Date: 2024-11-22 10:34:25
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2025-01-02 14:18:37
 * @Description: 设计的右侧面板
-->
<template>
  <div :class="[ 'hp-design-main-right', state.rightSideCollapse ? 'collapse' : '' ]" >
    <div :class="[ 'hp-aside-toggle', 'right', state.rightSideCollapse ? 'hidden' : '' ]"
      @click="state.rightSideCollapse = !state.rightSideCollapse"
    >
      <el-icon class="toggle-icon" >
        <CaretLeft v-show="state.rightSideCollapse"/>
        <CaretRight v-show="!state.rightSideCollapse"/>
      </el-icon>
    </div>
    <template v-if="attrConfigs.length">
      <div class="main-right-header">
        <div>属性配置【{{ selectedNode.name ?? '' }}】</div>
      </div>
      <div class="main-right-body">
        <el-scrollbar>
          <el-form label-width="90px" label-position="left">
            <el-collapse :modelValue="[1, 2]">
              <el-collapse-item v-for="(cfgType, cfgTypeIndex) in attrConfigs"
                :key="cfgTypeIndex"
                :name="cfgTypeIndex + 1"
                :title="cfgType.config.label"
              >
                <HpNodeItem
                  v-for="(item, index) in cfgType.children"
                  :key="index"
                  :config="item"
                  :schema="panelSchema"
                  :checkedNode="selectedNode"
                >
                </HpNodeItem>
              </el-collapse-item>
              <el-collapse-item title="大小定位" name="location">
                <el-form-item label="高度" >
                  <el-input-number :min="10" v-model="selectedNode.h"></el-input-number>
                </el-form-item>
                <el-form-item label="宽度" >
                  <el-input-number :min="10" v-model="selectedNode.w"></el-input-number>
                </el-form-item>
                <el-form-item label="X轴" >
                  <el-input-number :min="0" v-model="selectedNode.x"></el-input-number>
                </el-form-item>
                <el-form-item label="Y轴" >
                  <el-input-number :min="0" v-model="selectedNode.y"></el-input-number>
                </el-form-item>
              </el-collapse-item>
            </el-collapse>
          </el-form>
        </el-scrollbar>
      </div>
    </template>
    <template v-else>
      <ElEmpty description="请选择一个面板"/>
    </template>
  </div>
</template>

<script setup>
import { computed, inject, reactive, ref, watch } from 'vue'
import { ElEmpty } from 'element-plus'
import { CaretLeft, CaretRight } from '@element-plus/icons-vue'

import HpNodeItem from '@hp/design/src/node/NodeItem.vue'
import { pluginManager } from '@hp/utils/pluginManager.js'

const hpDesigner = inject('hpDesigner')

const { state } = hpDesigner

const panelSchema = ref({})
const attrConfigs = ref([])
const selectedNode = computed(() => state.checkedNode)

watch(
  () => state.checkedNode?.i,
  (nv) => {
    if (!nv) {
      attrConfigs.value = []
    } else {
      const { type, schema } = state.checkedNode
      const panelConfig = pluginManager.panelConfig[type] ?? []
      panelSchema.value = schema
      attrConfigs.value = panelConfig
    }
  },
  { deep: true, immediate: false }
)

</script>

<style lang="scss" scoped>
$border: 1px solid #d9d9d9;

.hp-design-main-right {
  width: 320px;
  transition: all .3s;
  border-left: $border;
  background-color: #ffffff;
  height: 100%;
  display: flex;
  flex-direction: column;
  transition: all .3s;
  &.collapse {
    width: 0;
  }
  .main-right-header {
    padding: 0 8px;
    height: 38px;
    display: flex;
    align-items: center;
    border-bottom: $border;
  }
  .main-right-body {
    flex: 1;
    min-height: 100px;
  }
  :deep(.el-collapse) {
    border-top: none;
  }
  :deep(.el-collapse-item__header) {
    --el-collapse-header-height: 38px;
    padding-left: 8px;
    background-color: rgba(0, 0, 0, 0.02);
    border-bottom: $border;
  }
  :deep(.el-collapse-item__wrap) {
    padding: 8px;
    border-bottom: $border;
    .el-collapse-item__content {
      padding-bottom: 0;
    }
    .el-form-item {
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
  :deep(.el-input-number) {
    width: 100%;
  }
}
</style>
